<template>
    <div>
        <!--新增-->
        <el-button-group>
            <el-button type="primary" @click="addVisible= true" style="margin-top: 10px" icon="el-icon-plus" >新增课程</el-button>
            <el-button type="primary" @click="addBatchVisible= true" style="margin-top: 10px" icon="el-icon-folder-add" >批量新增课程</el-button>
        </el-button-group>
        <!--搜索框-->
        <el-form :inline="true" class="demo-form-inline" :model="searchForm" :rules="searchFormRules" ref="searchForm" >
            <el-form-item label="">
                <el-select v-model="searchForm.semester" placeholder="开课学期"
                           style="width: 120px" clearable filterable>
                    <el-option
                            v-for="item in semesterData"
                            :key="item.id"
                            :label="item.season"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="time">
                <el-select v-model="searchForm.time" multiple placeholder="请选择上课时间" clearable filterable>
                    <el-option
                            v-for="item in timeData"
                            :key="item.id"
                            :label="item.show"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="location">
                <el-select v-model="searchForm.location" placeholder="教室"
                           style="width: 100px;" clearable filterable>
                    <el-option
                            v-for="item in locationData"
                            :key="item.id"
                            :label="item.courseLocation"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="">
                <el-input v-model="searchForm.courseId" placeholder="课程代码"></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-input v-model="searchForm.courseName" placeholder="课程名称"></el-input>
            </el-form-item>
            <el-form-item label="" prop="teacherId" label-width="100px">
              <el-select v-model="addForm.teacherId" placeholder="教师"
                         style="width: 200px" clearable filterable>
                <el-option
                    v-for="item in teacherData"
                    :key="item.id"
                    :label="item.name+'(工号：'+item.username+')'"
                    :value="item.username"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="submitSearch('searchForm')">搜索</el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <el-table
                ref="courseTable"
                :data="courseData"
                border
                stripe
                style="width: fit-content; margin-top: 10px">
            <el-table-column
                    prop="id"
                    label="课程序号"
                    width="50"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="semester"
                    label="授课学期"
                    width="100"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="courseId"
                    label="课程编号"
                    width="140"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="courseName"
                    label="课程名称"
                    width="100"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="课程类型"
                    width="100"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="schoolName"
                    label="授课院系"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="major"
                    label="授课专业"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="credit"
                    label="学分"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="hour"
                    label="学时"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="time"
                    label="上课时间"
                    width="90"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="location"
                    label="上课地点"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="teacher"
                    label="授课教师"
                    width="80"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="teacherId"
                    label="授课教师工号"
                    width="90"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="volume"
                    label="选课容量"
                    width=80
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="currentNum"
                    label="已选人数"
                    width=80
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="intro"
                    label="课程简介"
                    width="200"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button @click="memberListHandle(scope.row.courseId)" type="text" size="small">查看</el-button>
                    <el-button @click="editHandle(scope.row.id)" type="text" size="small">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.id)" >
                        <el-button type="text" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <!--新增课程弹窗-->
        <el-dialog title="请输入新增课程信息" :visible.sync="addVisible" @closed="resetAddForm('addForm')" width="600px">
            <!--表单-->
            <el-form :model="addForm" :rules="AddFormRules" ref="addForm" class="demo-dynamic">
                <el-form-item label="课程编号" prop="courseId" label-width="100px">
                    <el-input v-model="addForm.courseId" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程名称" prop="courseName" label-width="100px">
                    <el-input v-model="addForm.courseName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="授课学期" prop="semester" label-width="100px">
                    <el-select v-model="addForm.semester" placeholder="请选择授课学期" multiple clearable filterable>
                        <el-option
                                v-for="item in semesterData"
                                :key="item.id"
                                :label="item.season"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="课程类型" prop="type" label-width="100px">
                    <el-select v-model="addForm.type" placeholder="请选择课程种类" clearable filterable>
                        <el-option
                                v-for="item in typeData"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="授课院系" prop="school" label-width="100px">
                    <el-select v-model="addForm.school" placeholder="请选择院系" clearable filterable>
                        <!--                               @change="getMajorByCollege(addForm.school)"-->
                        <el-option
                                v-for="item in collegeData"
                                :key="item.id"
                                :label="item.college"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <!--                根据课程类型的选择决定是否显示专业-->
                <el-form-item
                        label="授课专业"
                        prop="major"
                        label-width="100px"
                        :rules=" {required: true, message: '请选择授课专业', trigger: 'blur'}"
                        v-if="addForm.type==1"
                >
                    <el-select v-model="addForm.major" placeholder="请选择" multiple clearable filterable>
                        <el-option
                                v-for="item in majorData"
                                :key="item.id"
                                :label="item.major"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学分" prop="credit" label-width="100px">
                    <el-input v-model="addForm.credit" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学时" prop="hour" label-width="100px">
                    <el-input v-model="addForm.hour" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上课时间" prop="time" label-width="100px">
                    <el-select v-model="addForm.time" multiple placeholder="请选择上课时间" clearable filterable>
                        <el-option
                                v-for="item in timeData"
                                :key="item.id"
                                :label="item.show"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <!--                <el-form-item label="上课时间" prop="time" label-width="100px">-->
                <!--                    <el-select placeholder="请选择上课时间" multiple collapse-tags v-model="timeitem" @change="getSelectDep">-->
                <!--                        <el-checkbox :style="selfStyle" v-model="checkedThing" @change='selectAllThing'>全选</el-checkbox>-->
                <!--                        <el-option v-for="(item, index) in timeL" :label="item.label" :value="item.label" :key="index"></el-option>-->
                <!--                    </el-select>-->
                <!--                </el-form-item>-->
                <el-form-item label="授课教室" prop="location" label-width="100px">
                    <el-select v-model="addForm.location" placeholder="请选择" clearable filterable>
                        <el-option
                                v-for="item in locationData"
                                :key="item.id"
                                :label="item.courseLocation"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <!--                <el-form-item label="授课教师" prop="teacher" label-width="100px">-->
                <!--                    <el-input v-model="addForm.teacher" autocomplete="off"></el-input>-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="教师工号" prop="teacherId" label-width="100px">-->
                <!--                    <el-input v-model="addForm.teacherId" autocomplete="off"></el-input>-->
                <!--                </el-form-item>-->
                <el-form-item label="授课教师" prop="teacherId" label-width="100px">
                    <el-select v-model="addForm.teacherId" placeholder="请选择" clearable filterable>
                        <el-option
                                v-for="item in teacherData"
                                :key="item.id"
                                :label="item.name+'(工号：'+item.username+')'"
                                :value="item.username"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选课容量" prop="volume" label-width="100px">
                    <el-input v-model="addForm.volume" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程简介" prop="intro" label-width="100px">
                    <el-input  type="textarea" v-model="addForm.intro" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <!--按钮-->
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetAddForm('addForm')">取 消</el-button>
                <el-button type="primary" @click="submitAddForm('addForm')">提交</el-button>
            </div>
        </el-dialog>

        <!--查看选课人员弹窗-->
        <el-dialog title="选课名单" :visible.sync="memberListVisible" @closed="resetmemberListTable('memberListTable')" width="800px">
            <el-table
                    ref="memberListTable"
                    :data="memberListData"
                    border
                    stripe
                    style="width: fit-content; margin-top: 10px">
                <el-table-column
                        prop="username"
                        label="学号"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="学生姓名"
                        width="100">
                </el-table-column>
                <!--                <el-table-column-->
                <!--                        prop="identificationNumber"-->
                <!--                        label="身份证号"-->
                <!--                        width="180">-->
                <!--                </el-table-column>-->
                <!--                <el-table-column-->
                <!--                        prop="phoneNumber"-->
                <!--                        label="学生手机号"-->
                <!--                        width="120">-->
                <!--                </el-table-column>-->
                <el-table-column
                        prop="email"
                        label="学生邮箱"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="collegeName"
                        label="所属院系"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="majorName"
                        label="所属专业"
                        width="120">
                </el-table-column>
            </el-table>
        </el-dialog>

        <!--编辑课程弹窗-->
        <el-dialog title="编辑课程信息" :visible.sync="editCourseVisible" @closed="resetEditForm('editForm')" width="600px">
            <el-form :model="editForm" :rules="editFormRules" ref="editForm">
                <el-form-item label="课程序号" prop="id" label-width="100px">
                    <el-input v-model="editForm.id" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="授课学期" prop="semester" label-width="100px">
                    <el-input v-model="editForm.semester" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程编号" prop="courseId" label-width="100px">
                    <el-input v-model="editForm.courseId" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程名称" prop="courseName" label-width="100px" >
                    <el-input v-model="editForm.courseName" autocomplete="off" @input="change($event)"></el-input>
                </el-form-item>
                <el-form-item label="课程类型" prop="type" label-width="100px">
                    <el-input v-model="editForm.type" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="院系" prop="schoolName" label-width="100px">
                    <el-input v-model="editForm.schoolName" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="专业" prop="major" label-width="100px">
                    <el-input v-model="editForm.major" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学分" prop="credit" label-width="100px">
                    <el-input v-model="editForm.credit" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="学时" prop="hour" label-width="100px">
                    <el-input v-model="editForm.hour" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上课时间" prop="time" label-width="100px">
                    <el-select v-model="editForm.time" multiple placeholder="请选择上课时间" clearable filterable @input="change($event)">
                        <el-option
                                v-for="item in timeData"
                                :key="item.id"
                                :label="item.show"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上课地点" prop="location" label-width="100px">
                    <el-select v-model="editForm.location" placeholder="请选择" clearable filterable @input="change($event)">
                        <el-option
                                v-for="item in locationData"
                                :key="item.id"
                                :label="item.courseLocation"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="授课教师" prop="teacher" label-width="100px">
                    <el-input v-model="editForm.teacher" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="授课教师工号" prop="teacherId" label-width="110px">
                    <el-input v-model="editForm.teacherId" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="选课容量" prop="volume" label-width="100px">
                    <el-input v-model="editForm.volume" @input="change($event)" validate-on-rule-change autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程简介" prop="intro" label-width="100px">
                    <el-input v-model="editForm.intro" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetEditForm('editForm')">取 消</el-button>
                <el-button type="primary" @click="submitEditForm('editForm')">保 存</el-button>
            </div>
        </el-dialog>

        <!--批量新增成员对话框-->
        <el-dialog title="请选择批量新增课程的文件" :visible.sync="addBatchVisible" width="600px" @closed="resetMoreAddForm('moreAddForm')">
            <input type="file" accept=".csv" @change="importcsv"/>
        </el-dialog>

    </div>
</template>

<script>
    import qs from 'qs'
    export default {
        name: "Courses",
        data() {
            var validateAddVolume = (addFormRules,value,callback)=>{
                let locationId=this.addForm.location
                let locationItem=this.locationData.find(item=>item.id===locationId)
                // console.log('locationData',this.locationData)
                // console.log('value',value)
                let locationVolume=locationItem.volume
                if (value > locationVolume){
                    callback(new Error("课程容量超过教室容量，该教室容量为"+locationVolume));
                }
                else {
                    callback();
                }
            }
            var validateEditVolume = (editFormRules,value,callback)=>{
                let courseId=this.editForm.id
                let locationId=this.editForm.location
                let currentNum=this.courseData[courseId-1].currentNum
                let locationItem=this.locationData.find(item=>item.id===locationId)
                let locationVolume=locationItem.volume
                console.log('value',value)
                console.log('currrentNum',currentNum)
                console.log('locationVolume',locationVolume)
                if (value <= locationVolume && value >= currentNum){
                    console.log('success')
                    callback();
                }
                else {
                    if (value < currentNum){
                        callback(new Error("课程容量小于已选人数，请扩容"));
                    }
                    else {
                        if (value>locationVolume) {
                            callback(new Error("课程容量超过教室容量，该教室容量为" + locationVolume));
                        }
                        else callback();
                    }
                }
            }

            return {
                typeData:[
                    {label:"通选课程",value:0},
                    {label:"专选课程",value:1},
                    // {label:"专业进阶课程",value:2},
                ],
                //表格数据
                addForm: {},
                moreAddForm: {},
                editForm: {},
                searchForm:{},

                courseData: [],
                collegeData:[],
                majorData:[],
                locationData:[],
                timeData:[],
                semesterData:[],
                memberListData:[],
                teacherData:[],

                addVisible: false,
                editCourseVisible: false,
                addBatchVisible: false,
                memberListVisible:false,

                checkedThing: false,
                time:[],

                timeL:[
                    {value: '1',label: '周一 8：00-8：45'},
                    {value: '2', label: '周一 8：55-9：40'},
                ],

                //提交表单规则
                AddFormRules:{
                    courseName: [
                        {required: true, message: '请输入课程名称', trigger: 'blur'},
                        // {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "课程名称是中英文", trigger: "blur"}
                    ],
                    courseId: [
                        {required: true, message: '请输入课程编码', trigger: 'blur'},
                        // {pattern: /^[0-9]*$/, message: "教室编码是数字", trigger: "blur"}
                    ],
                    semester:[
                        {required: true, message: '请选择课程类型', trigger: 'blur'},
                    ],
                    type:[
                        {required: true, message: '请选择课程类型', trigger: 'blur'},
                    ],
                    school: [
                        {required: true, message: '请选择开课院系', trigger: 'blur'},
                    ],
                    credit: [
                        {required: true, message: '请输入课程学分', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "学分是正整数", trigger: "blur"}
                    ],
                    hour: [
                        {required: true, message: '请输入课程学时', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "学时是正整数", trigger: "blur"}
                    ],
                    // major: [
                    //     {required: true, message: '请选择授课专业', trigger: 'blur'},
                    //     // {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "专业名是中英文", trigger: "blur"}
                    // ],
                    teacher: [
                        {required: true, message: '请输入教师姓名', trigger: 'blur'},
                        {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "教师姓名是中英文", trigger: "blur"}
                    ],
                    teacherId:[
                        {required: true, message: '请输入教师工号', trigger: 'blur'},
                    ],
                    intro: [],
                    volume: [
                        {required: true, message: '请输入最大选课人数', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "最大选课人数需是正整数", trigger: "blur"},
                        {validator:validateAddVolume, trigger:"blur"},
                    ],
                    time: [
                        {required: true, message: '请输入上课时间', trigger: 'blur'},
                        // {pattern: /^([A-Z]+[0-9]*)+$/, message: "用逗号分割数字", trigger: "blur"}
                    ],
                    location: [
                        {required: true, message: '请选择授课教室', trigger: 'blur'},
                    ],

                },
                editFormRules:{
                    id:[
                        {required: true, message: '请输入课程序号', trigger: 'blur'},
                    ],
                    courseName: [
                        {required: true, message: '请输入课程名称', trigger: 'blur'},
                        // {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "课程名称是中英文", trigger: "blur"}
                    ],
                    courseId: [
                        {required: true, message: '请输入课程编码', trigger: 'blur'},
                        // {pattern: /^[0-9]*$/, message: "教室编码是数字", trigger: "blur"}
                    ],
                    semester:[
                        {required: true, message: '请选择课程类型', trigger: 'blur'},
                    ],
                    type:[
                        {required: true, message: '请选择课程类型', trigger: 'blur'},
                    ],
                    schoolName: [
                        {required: true, message: '请选择开课院系', trigger: 'blur'},
                    ],
                    credit: [
                        {required: true, message: '请输入课程学分', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "学分是正整数", trigger: "blur"}
                    ],
                    hour: [
                        {required: true, message: '请输入课程学时', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "学时是正整数", trigger: "blur"}
                    ],
                    major: [
                        {required: true, message: '请选择授课专业', trigger: 'blur'},
                        {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "专业名是中英文", trigger: "blur"}
                    ],
                    teacher: [
                        {required: true, message: '请输入教师姓名', trigger: 'blur'},
                        {pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: "教师姓名是中英文", trigger: "blur"}
                    ],
                    teacherId:[
                        {required: true, message: '请输入教师工号', trigger: 'blur'},
                    ],
                    intro: [],
                    volume: [
                        {required: true, message: '请输入最大选课人数', trigger: 'blur'},
                        {pattern: /^[1-9]\d*$/, message: "最大选课人数需是正整数", trigger: "blur"},
                        {validator:validateEditVolume, trigger:"blur"},
                    ],
                    time: [
                        {required: true, message: '请输入上课时间', trigger: 'blur'},
                        // {pattern: /^([A-Z]+[0-9]*)+$/, message: "用逗号分割数字", trigger: "blur"}
                    ],
                    location: [
                        {required: true, message: '请选择授课教室', trigger: 'blur'},
                    ],

                },
                searchFormRules:{},

                // selfStyle: {
                //     textAlign: "right",
                //     width: "100%",
                //     paddingRight: "10px",
                // },
            }
        },
        created(){
            this.getCourseList();
            this.getCollegeList();
            this.getLocationList();
            this.getTimeList();
            this.getSemesterList();
            this.getMajorList();
            this.getTeacherList();
        },
        methods:{
            //获取课程列表
            getCourseList(){
                this.$axios.get('/sys/course/list').then(res => {
                    // var newData=[]
                    // for (var i=0;i<res.data.data.length;i++) {
                    //     //判断课程状态
                    //     if (res.data.data[i].statu === 0) {
                    //         var data = {}
                    //         var mapTime = [], mapSemester = [], mapLocation = [], mapMajor = []
                    //         data.id = res.data.data[i].id
                    //
                    //         for (var key in res.data.data[i].semester) {
                    //             mapSemester.push(res.data.data[i].semester[key])
                    //             mapSemester.push(',')
                    //         }
                    //         mapSemester.pop()
                    //         data.semester = mapSemester
                    //
                    //         data.courseId = res.data.data[i].courseId
                    //         data.courseName = res.data.data[i].courseName
                    //         if (res.data.data[i].type === 0) {
                    //             data.type = '通选课程'
                    //         } else {
                    //             if (res.data.data[i].type === 1)
                    //                 data.type = '专选课程'
                    //         }
                    //
                    //         //得到数据时直接得到schoolName
                    //         data.schoolName = res.data.data[i].schoolName
                    //
                    //         for (var key in res.data.data[i].major) {
                    //             mapMajor.push(res.data.data[i].major[key])
                    //             mapMajor.push(',')
                    //         }
                    //         mapMajor.pop()
                    //         data.major = mapMajor
                    //
                    //         data.credit = res.data.data[i].credit
                    //         data.hour = res.data.data[i].hour
                    //
                    //         for (var key in res.data.data[i].time) {
                    //             mapTime.push(res.data.data[i].time[key])
                    //             mapTime.push(',')
                    //         }
                    //         mapTime.pop()
                    //         data.time = mapTime
                    //
                    //         // for (var key in res.data.data[i].location){
                    //         //     mapLocation.push(res.data.data[i].location[key])
                    //         //     mapLocation.push(',')
                    //         // }
                    //         // mapLocation.pop()
                    //         // data.location = mapLocation
                    //         if (res.data.data[i].location !== null) {
                    //             data.location = res.data.data[i].location.courseLocation
                    //         }
                    //
                    //         data.teacher = res.data.data[i].teacher
                    //         data.teacherId = res.data.data[i].teacherId
                    //         data.intro = res.data.data[i].intro
                    //         data.volume = res.data.data[i].volume
                    //         data.currentNum = res.data.data[i].currentNum
                    //
                    //         newData.push(data)
                    //     }
                    //     // console.log('courseData',this.courseData)
                    // }
                    this.courseData = this.getCourseData(res)
                })
            },
            //获取学院列表
            getCollegeList() {
                this.$axios.get('/sys/college/list').then(res => {
                    this.collegeData = res.data.data
                })
            },
            //获得教室列表
            getLocationList(){
                this.$axios.get("/sys/location/list").then(res => {
                    // console.log('LocationList',res.data.data)
                    this.locationData = res.data.data;
                })
            },
            //获取上课时间列表
            getTimeList(){
                this.$axios.get("/sys/time/listAll").then(res => {
                    // console.log('TimeList',res.data.data)
                    this.timeData = res.data.data;
                })
            },
            //获取学期列表
            getSemesterList(){
                this.$axios.get("/sys-semester/listAll").then(res => {
                    // console.log('SemesterList',res.data.data)
                    this.semesterData= res.data.data;
                })
            },
            //根据之前选择的院系返回专业
            // getMajorByCollege(college) {
            //     console.log('school',college)
            //     this.$axios.post('/sys/major/listByCollege?', {id: college}).then(res => {
            //         // alert("选择了新的院系，现在在选择专业")
            //         console.log('majorData',res.data.data)
            //         this.majorData = res.data.data
            //     })
            // },
            getMajorList(){
                this.$axios.get("/sys/major/list").then(res => {
                    // console.log('SemesterList',res.data.data)
                    this.majorData= res.data.data;
                })
            },
            getTeacherList(){
                this.$axios.get("/sys/user/teacherList").then(res => {
                    // console.log('teacherList',res.data.data)
                    this.teacherData= res.data.data;
                    // console.log('teacherData',this.teacherData)
                })
            },
            //解决赋值给editform后无法编辑问题
            change(e){
                this.$forceUpdate();
            },
            //查看课程选课人员
            memberListHandle(courseId){
                this.$axios.post("/sys/course/memberList?",{courseId:courseId}).then(res=>{
                    // console.log('mem',res.data.data)
                    this.memberListData=res.data.data.sysUsers
                    this.memberListVisible=true

                })
            },
            //手动编辑
            editHandle(id) {
                this.$axios.post("/sys/course/info?",{id:id}).then(res => {

                    // this.editForm= res.data.data
                    var mapTime=[],mapSemester=[],mapMajor=[]
                    this.editForm.id = res.data.data.id

                    for (var key in res.data.data.semester){
                        mapSemester.push(res.data.data.semester[key])
                    }
                    this.editForm.semester=mapSemester

                    this.editForm.courseId = res.data.data.courseId
                    this.editForm.courseName = res.data.data.courseName
                    if(res.data.data.type===0){
                        this.editForm.type='通选课程'
                    }
                    else {
                        if (res.data.data.type===1)
                            this.editForm.type='专选课程'
                    }

                    //得到数据时直接得到schoolName
                    this.editForm.schoolName = res.data.data.schoolName

                    for (var key in res.data.data.major){
                        mapMajor.push(res.data.data.major[key])
                    }
                    this.editForm.major = mapMajor

                    this.editForm.credit = res.data.data.credit
                    this.editForm.hour = res.data.data.hour

                    for (var key in res.data.data.time){
                        mapTime.push(res.data.data.time[key])
                    }
                    this.editForm.time=mapTime

                    // for (var key in res.data.data[i].location){
                    //     mapLocation.push(res.data.data[i].location[key])
                    //     mapLocation.push(',')
                    // }
                    // mapLocation.pop()
                    // data.location = mapLocation
                    if (res.data.data.location!==null) {
                        this.editForm.location = res.data.data.location.id
                    }
                    this.editForm.teacher = res.data.data.teacher
                    this.editForm.teacherId = res.data.data.teacherId
                    this.editForm.intro = res.data.data.intro
                    // this.editForm.volume = res.data.data.volume

                    this.editCourseVisible = true
                })
            },
            //手动删除
            delHandle(id) {
                this.$axios.post("/sys/course/delete/admin?",{id:id}).then(res => {
                    console.log(res)
                    this.$message({
                        showClose: true,
                        message: '删除成功',
                        type: 'success',
                        onClose: () => {
                            this.getCourseList()
                            //刷新一下
                            window.location.reload();
                        }
                    });
                })
            },
            //提交新增表单
            submitAddForm(formName) {
                // const timeli=this.addForm.time.join(",")
                // console.log(timeli)
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // console.log('mapMajor',mapMajor)
                        this.$axios({
                            method:'POST',
                            url:'sys/course/save',
                            data:{
                                semesterId:this.addForm.semester,
                                courseId:this.addForm.courseId,
                                courseName:this.addForm.courseName,
                                type:this.addForm.type,
                                school:this.addForm.school,
                                majorId:this.addForm.major,
                                credit:this.addForm.credit,
                                hour:this.addForm.hour,
                                timeId:this.addForm.time,
                                locationId:this.addForm.location,
                                // teacher:this.addForm.teacher,
                                teacherId:this.addForm.teacherId,
                                intro:this.addForm.intro,
                                volume:this.addForm.volume,
                                currentNum:0
                            },
                        }).then(res => {
                            this.resetAddForm(formName)
                            this.$message({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success',
                                onClose: () => {
                                    this.getCourseList();
                                    location.reload();
                                }
                            });
                            this.addVisible = false;
                            this.majorData = '';
                        })
                    } else {
                        console.log('error submit!');
                        return false;
                    }
                });
            },
            //提交编辑表单
            submitEditForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const schoolItem=this.collegeData.find(item=>item.college===this.editForm.schoolName)
                        const schoolId=schoolItem.id
                        var typeId
                        if (this.editForm.type==="通选课程") typeId=0
                        else {
                            typeId=1
                        }

                        var majorList=new Array()
                        var semesterList=new Array()

                        this.$axios.post("/sys/course/info?",{id:this.editForm.id}).then(res1 => {
                            for (var key in res1.data.data.major){
                                majorList.push(key-0)
                            }
                            for (var key in res1.data.data.semester){
                                semesterList.push(key-0)
                            }
                        })
                        // const semesterItem=this.semesterData.find(item=>item.season===this.editForm.semester)
                        // const semester_Id=semesterItem.id
                        // this.editForm.major.split(',')
                        // var majorString=this.editForm.major
                        // var majorList=[]
                        // for (var i=0;i<majorString.length;i++){
                        //     var majorItem=this.majorData.find(item=>item.major===majorString[i])
                        //     majorList.push(majorItem.id)
                        // }
                        // console.log('semesterList',semesterList)
                        // console.log('majorList',majorList)
                        console.log('time',this.editForm.time)
                        setTimeout(()=> {
                            this.$axios({
                                method: 'POST',
                                url: 'sys/course/update/admin',
                                data: {
                                    id:this.editForm.id,
                                    semesterId: semesterList,
                                    courseId: this.editForm.courseId,
                                    courseName: this.editForm.courseName,
                                    type: typeId,
                                    school: schoolId,
                                    majorId: majorList,
                                    credit: this.editForm.credit,
                                    hour: this.editForm.hour,
                                    timeId: this.editForm.time,
                                    locationId: this.editForm.location,
                                    teacher: this.editForm.teacher,
                                    teacherId: this.editForm.teacherId,
                                    intro: this.editForm.intro,
                                    volume: this.editForm.volume
                                },
                            }).then(res => {
                                console.log('semesterList', semesterList)
                                console.log('majorList', majorList)
                                this.resetEditForm(formName)
                                this.$message({
                                    showClose: true,
                                    message: '恭喜你，编辑成功',
                                    type: 'success',
                                    onClose: () => {
                                        this.getCourseList();
                                        // location.reload();
                                    }
                                });
                                this.addVisible = false;
                                this.majorData = '';
                            })
                        },100)
                    } else {
                        console.log('error submit!');
                        return false;
                    }
                });
            },

            //重设新增课程表单
            resetAddForm(formName){
                this.$refs[formName].resetFields();
                this.addForm = {}
                this.addVisible = false
            },
            //重设批量新增课程表单
            resetMoreAddForm(formName){
                this.$refs[formName].resetFields();
                this.moreAddForm = {}
                this.addBatchVisible = false
            },
            //重设选课人员表格
            resetmemberListTable(tableName){
                this.$refs[tableName].resetFields();
                this.memberListData=[]
                this.memberListVisible=false
            },
            //重设修改课程表单
            resetEditForm(formName){
                this.$refs[formName].resetFields();
                this.editForm = {}
                this.editCourseVisible = false
            },
            // getSelectDep(timeitem) {
            //     debugger
            //
            //     if (timeitem.length === this.timeL.length) {
            //         this.checkedThing = true
            //     } else {
            //         this.checkedThing = false
            //     }
            // },
            //
            // selectAllThing() {
            //     debugger
            //     this.timeitem = []
            //     if (this.checkedThing) {
            //         this.timeL.map((item) => {
            //             this.timeitem.push(item.label)
            //         })
            //     } else {
            //         this.department = []
            //     }
            // },

            importcsv: function(obj) {
                // let obj=file.raw
                let that = this//如果需要点击事件结束之后对DOM进行操作使用)_this.xx=xx进行操作
                this.$papa.parse(obj.target.files[0], {
                    complete:function (results) {
                        console.log(results)//这个是csv文件的数据

                        // let data = []
                        for (let i = 1; i < results.data.length; i++) {
                            let obj=new FormData()

                            obj.append("courseName",results.data[i][0])
                            obj.append("courseId",results.data[i][1])
                            obj.append("school",results.data[i][2])
                            obj.append("credit",results.data[i][3])
                            obj.append("hour",results.data[i][4])
                            obj.append("major",results.data[i][5])
                            obj.append("teacher",results.data[i][6])
                            obj.append("teacherId",results.data[i][7])
                            obj.append("intro",results.data[i][8])
                            obj.append("volume",results.data[i][9])
                            obj.append("location",results.data[i][10])
                            obj.append("timeId",results.data[i][11])

                            // console.log('2')
                            // console.log(obj)
                            that.$axios.post('/sys/course/bat',obj)
                        }
                        // data.splice(0, 1)//将数组第一位的表格名称去除
                        // let num = 0
                        // console.log('data', data)
                        /* _this.tableData = data//将数据放入要展示的表格中*/
                    }
                });
            },

            //处理课程信息的统一方法
            //输入返回值，输出处理好的数组
            getCourseData(res){
              var dataArray=[];
              for (var i=0;i<res.data.data.length;i++) {
                  if (res.data.data[i].statu === 0) {
                      var data = {}
                      var key;
                      var mapTime = [], mapSemester = [], mapLocation = [], mapMajor = []
                      data.id = res.data.data[i].id
                      for (key in res.data.data[i].semester) {
                          mapSemester.push(res.data.data[i].semester[key])
                          mapSemester.push(',')
                      }
                      mapSemester.pop()
                      data.semester = mapSemester
                      data.courseId = res.data.data[i].courseId
                      data.courseName = res.data.data[i].courseName
                      data.type = (res.data.data[i].type === 0) ? '通选课程' : '专选课程';

                      data.schoolName = res.data.data[i].schoolName;
                      for (key in res.data.data[i].major) {
                          mapMajor.push(res.data.data[i].major[key])
                          mapMajor.push(',')
                      }
                      mapMajor.pop()
                      data.major = mapMajor;
                      data.credit = res.data.data[i].credit;
                      data.hour = res.data.data[i].hour;
                      for (var key in res.data.data[i].time) {
                          mapTime.push(res.data.data[i].time[key])
                          mapTime.push(',')
                      }
                      mapTime.push()
                      data.time = mapTime

                      // for (var key in res.data.data[i].location){
                      //     mapLocation.push(res.data.data[i].location[key])
                      //     mapLocation.push(',')
                      // }
                      // mapLocation.pop()
                      // data.location = mapLocation
                      if (res.data.data[i].location !== null) {
                          data.location = res.data.data[i].location.courseLocation
                      }
                      data.teacher = res.data.data[i].teacher;
                      data.teacherId = res.data.data[i].teacherId;
                      data.intro = res.data.data[i].intro;
                      data.volume = res.data.data[i].volume;
                      data.currentNum = res.data.data[i].currentNum;
                      data.courseNum = data.currentNum + '/' + data.volume;
                      dataArray.push(data);
                  }
              }
              return dataArray;
            },
            //搜索课程
            submitSearch(formName){
              this.$refs[formName].validate((valid) => {
                if (valid) {
                  console.log(this.searchForm)
                  this.$axios.post('sys/course/searchAll', this.searchForm).then(res => {
                    this.courseData=this.getCourseData(res);
                    console.log("搜索结果：",this.courseData);
                    this.$message({
                      showClose: true,
                      message: '搜索成功',
                      type: 'success',
                      onClose: () => {
                        this.resetSearch(formName);
                      }
                    });
                  })
                } else {
                  console.log('搜索失败!');
                  return false;
                }
              });
            },

        }
    }
</script>

<style scoped>
    .demo-form-inline{
        padding-top: 15px;
    }
</style>